package com.zte.claa.inficombo.csa.app.service.rptparse;

import java.nio.ByteBuffer;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GspSubCrossMPKData {
	private String sensorId;
	private int totalNum = 0;
	private long rptTime = 0;
	private int msgid = 0;
	// 数据包序号：约定从1开始，并且连续...
	private Map<Integer, byte[]> datamap;
	
	public GspSubCrossMPKData(){
		this.datamap = new HashMap<Integer, byte[]>();
	}

	public String getSensorId() {
		return sensorId;
	}

	public void setSensorId(String sensorId) {
		this.sensorId = sensorId;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public long getRptTime() {
		return rptTime;
	}

	public void setRptTime(long rptTime) {
		this.rptTime = rptTime;
	}
	
	public Map<Integer, byte[]> getDatamap() {
		return datamap;
	}

	public void setDatamap(Map<Integer, byte[]> datamap) {
		this.datamap = datamap;
	}

	public int getMsgid() {
		return msgid;
	}

	public void setMsgid(int msgid) {
		this.msgid = msgid;
	}

	// 判断数据初次上报时间是否已经超过指定时长.
	public boolean isDataExpire(long expireMillSecond) {
		return ((System.currentTimeMillis() - this.rptTime) >= expireMillSecond);
	}
	
	/**
	 * 添加数据。
	 * 
	 * @param pkgNo
	 * @param data
	 * @return： 1--数据包重复；
	 *          0--数据包不重复；
	 */
	public int appendData(int pkgNo, byte[] data) {
		int ret = 0;
		if(this.datamap.get(pkgNo) != null){
			ret = 1;
		}
		this.datamap.put(pkgNo, data);
		return ret;
	}
	
	// 判断data是否收全了.
	public boolean isDataOk(){
		return this.datamap.size() == this.totalNum;
	}
	
	/**
	 * 拼接数据流，返回完整结果.
	 * @return
	 */
	public byte[] buildData() {
		int totalLen = 0;
		Iterator<Integer> it = this.datamap.keySet().iterator();
		while(it.hasNext()){
			Integer key = it.next();
			byte[] bs = this.datamap.get(key);
			if(bs == null){
				continue;
			}
			totalLen += bs.length;
		}		
		if(totalLen == 0){
			return new byte[0];
		}
		// 按key的值从小到大顺序依次读取byte[]...  
		ByteBuffer bb = ByteBuffer.allocate(totalLen);
		List<Integer> keyList = new ArrayList<Integer>();
		keyList.addAll(this.datamap.keySet());		
		// a - b 升序.
		keyList.sort((a, b) -> (a.compareTo(b)));
		int len = keyList.size();
		for(int i=0; i<len; i++){
			byte[] bs = this.datamap.get(keyList.get(i));
			if(bs == null){
				continue;
			}
			bb.put(bs);
		}	
		return bb.array();
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("sensorId=").append(sensorId).append(",");
		sb.append("rptTime=").append(new Date(rptTime).toString()).append(",");
		sb.append("msgid=").append(msgid).append(",");
		sb.append("totalNum=").append(totalNum).append(",");
		sb.append("curPkgNum=").append(this.datamap.size()).append(": ");		
		Iterator<Integer> it = this.datamap.keySet().iterator();
		while(it.hasNext()){
			Integer itKey = it.next();
			byte[] bs = this.datamap.get(itKey);
			int len = (bs == null) ? 0 : bs.length;
			sb.append("key[").append(itKey).append("].datalen=[").append(len).append("]; ");
		}
		return sb.toString();		
	}
	
	/**
	public static void main(String[] args){
		GspSubCrossMPKData d = new GspSubCrossMPKData();
		d.appendData(2, new byte[]{1, 2, 3});
		d.appendData(3, new byte[]{10, 20, 30});
		d.appendData(4, new byte[]{41, 42, 43});
		byte[] bs = d.buildData();
		for(int i=0; i<bs.length; i++){
			System.out.println("bs["+i+"]="+bs[i]);
		}
	}
	*/
}
